package com.teaching.management.service.impl;

import com.teaching.management.entity.CourseSelection;
import com.teaching.management.entity.Student;
import com.teaching.management.repository.CourseSelectionRepository;
import com.teaching.management.repository.StudentRepository;
import com.teaching.management.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private CourseSelectionRepository courseSelectionRepository;

    @Override
    public Student getStudentByUserId(Integer userId) {
        return studentRepository.findByUserId(userId);
    }

    @Override
    public List<CourseSelection> getStudentCourses(String studentId) {
        return courseSelectionRepository.findByStudentId(studentId);
    }

    @Override
    public Map<String, Object> getStudyOverview(String studentId) {
        Map<String, Object> overview = new HashMap<>();

        // 获取学生学分
        Integer totalCredits = getTotalCredits(studentId);
        if (totalCredits == null) {
            totalCredits = 0;
        }

        // 获取当前学期课程数
        int currentCourseCount = getCurrentCourseCount(studentId);

        // 获取平均成绩
        BigDecimal avgScore = getAverageScore(studentId);

        overview.put("totalCredits", totalCredits);
        overview.put("currentCourseCount", currentCourseCount);
        overview.put("averageScore", avgScore);

        return overview;
    }

    @Override
    public BigDecimal getAverageScore(String studentId) {
        return courseSelectionRepository.getAvgScoreByStudentId(studentId);
    }

    @Override
    public Integer getTotalCredits(String studentId) {
        return courseSelectionRepository.getTotalCreditsByStudentId(studentId);
    }

    @Override
    public int getCurrentCourseCount(String studentId) {
        return courseSelectionRepository.countByStudentIdAndStatus(studentId, "正在修");
    }
}